using System.Collections.Generic;
using System.Threading.Tasks;
using JoyfulHomeJourney.BackEnd.Infrastructure.Atrributes;
using JoyfulHomeJourney.BackEnd.Infrastructure.Dtos.Requests.Objects;
using JoyfulHomeJourney.BackEnd.Infrastructure.Dtos.Responses;
using JoyfulHomeJourney.BackEnd.Infrastructure.Dtos.Responses.Objects;
using JoyfulHomeJourney.BackEnd.Infrastructure.EFCore;
using JoyfulHomeJourney.BackEnd.Server.EFCore;
using JoyfulHomeJourney.BackEnd.Server.Instances.Extends.Requests;
using JoyfulHomeJourney.BackEnd.Server.Instances.Extends.Responses;
using JoyfulHomeJourney.BackEnd.Server.Interfaces;
using Microsoft.EntityFrameworkCore;
namespace JoyfulHomeJourney.BackEnd.Server.Instances
{
    /// <summary>
    /// 一个类包含了服务类所需的所有仓库表的实例，仅提供给该服务类使用，请将所有服务类所需的仓库表的实例都放在该类中。
    /// 仓库表的实例指提供了操作数据库指定表的所有方法的对象
    /// </summary>
    internal class SysDictionaryTypesEF
    {
        public SysDictionaryTypesEF(DbContext dbContext)
        {
            sysDictionaryTypesServerEF = new SysDictionaryTypesServerEF(dbContext);
            dictionarysServerEF = new DictionarysServerEF(dbContext);
            sysAdminsServerEF = new SysAccountsServerEF(dbContext);
        }
        public SysDictionaryTypesServerEF sysDictionaryTypesServerEF { get; set; }
        public DictionarysServerEF dictionarysServerEF { get; set; }
        public SysAccountsServerEF sysAdminsServerEF { get; set; }
    }
    [InjectClass]
    public class SysDictionaryTypesServer : ISysDictionaryTypesServer
    {
        private readonly SysDictionaryTypesEF _sysDictionaryTypesEF;//包含所有所需仓库表实现的对象
                                                                    // 请不要修改该构造方法,也无需添加属性，而应该在上面的EF类中添加属性和构造函数赋值语句
        public SysDictionaryTypesServer(DbContext dbContext)
        {
            _sysDictionaryTypesEF = new SysDictionaryTypesEF(dbContext);
        }
        private async Task<bool> Exist(string id)
        {
            return await _sysDictionaryTypesEF.sysDictionaryTypesServerEF.Query(d => d.DtId == id).AnyAsync();
        }
        private async Task<bool> ExistChildren(string id)
        {
            return await _sysDictionaryTypesEF.dictionarysServerEF.Query(d => d.DDictionaryTypeId == id).AnyAsync();
        }
        public async Task<ResponseApiDto> AddAsync(RequestSysDictionaryTypesDto dto)
        {
            return ResponseApiDto.Create(await _sysDictionaryTypesEF.sysDictionaryTypesServerEF.AddAsync(dto.ToCreateEntity()));
        }
        public async Task<ResponseApiDto> DeleteAsync(RequestSysDictionaryTypesDto dto)
        {
            if (!await Exist(dto.id))
            {
                return ResponseApiDto.Fail("字典不存在");
            }
            if (await ExistChildren(dto.id))
            {
                return ResponseApiDto.Fail("请先删除字典子级");
            }
            return ResponseApiDto.Create(await _sysDictionaryTypesEF.sysDictionaryTypesServerEF.DeleteAsync(dto.ToDeleteEntity()));
        }
        public async Task<ResponseApiDto> UpdateAsync(RequestSysDictionaryTypesDto dto)
        {
            if (!await Exist(dto.id))
            {
                return ResponseApiDto.Fail("字典不存在");
            }
            return ResponseApiDto.Create(await _sysDictionaryTypesEF.sysDictionaryTypesServerEF.UpdateAllProperNotNullAsync(dto.ToUpdateEntity()));
        }
        public async Task<ResponseApiDto> PaginationAsync(int page, int limit, string key)
        {
            int total = 0;
            List<ResponseSysDictionaryTypesDto> data = new List<ResponseSysDictionaryTypesDto>();
            List<SysDictionaryTypes> sysDictionaryTypess = await _sysDictionaryTypesEF.sysDictionaryTypesServerEF.Pagination1(d => key == null || d.DtName.Contains(key) || d.DtKey.Contains(key), false, d => d.DtCreateTime, out total, page, limit).ToListAsync();
            foreach (var sysDictionaryTypes in sysDictionaryTypess)
            {
                var dictionaryType = sysDictionaryTypes.ToResponseDto();
                dictionaryType.authName = (await _sysDictionaryTypesEF.sysAdminsServerEF.Query(d => d.AId == sysDictionaryTypes.DtAuth).SingleOrDefaultAsync())?.AName;
                dictionaryType.subCount = await _sysDictionaryTypesEF.dictionarysServerEF.Query(d => d.DDictionaryTypeId == sysDictionaryTypes.DtId).CountAsync();
                data.Add(dictionaryType);
            }
            return ResponseApiDto.Success("数据已获取", new ResponsePaginationDto { total = total, data = data });
        }
    }
}
